package com.leilei.entity;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * @author lei
 * @create 2022-06-05 11:51
 * @desc 统一响应对象
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResultVO<T> {

    private Integer code;

    private String message;

    private T data;


    public boolean checkResult() {
        return Objects.equals(code, ResponseCodeEnum.SUCCESS.getCode());
    }

    public static <T> ResultVO<T> ok(T data) {
        return ResultVO.<T>builder()
                .code(ResponseCodeEnum.SUCCESS.getCode())
                .message(ResponseCodeEnum.SUCCESS.getMessage())
                .data(data)
                .build();
    }

    public static <T> ResultVO<T> ok() {
        return ResultVO.<T>builder()
                .code(ResponseCodeEnum.SUCCESS.getCode())
                .message(ResponseCodeEnum.SUCCESS.getMessage())
                .data(null)
                .build();
    }

    public static <T> ResultVO<T> fail() {
        return ResultVO.<T>builder()
                .code(ResponseCodeEnum.FAIL.getCode())
                .message(ResponseCodeEnum.FAIL.getMessage())
                .data(null)
                .build();
    }

    public static <T> ResultVO<T> failMessage(String message) {
        return ResultVO.<T>builder()
                .code(ResponseCodeEnum.FAIL.getCode())
                .message(message)
                .data(null)
                .build();
    }

    public static <T> ResultVO<T> failMessage(Integer code, String message) {
        return ResultVO.<T>builder()
                .code(code)
                .message(message)
                .data(null)
                .build();
    }

    public static ResultVO<Object> enumData(ResponseCodeEnum responseCodeEnum) {
        return ResultVO.<Object>builder().code(responseCodeEnum.getCode()).message(responseCodeEnum.getMessage()).build();
    }
}
